﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Timers;
using AutoProcessService.Interface;

namespace AutoProcessService.Biz
{
    public static class Core
    {
        static Dictionary<string, ExecutorInfo> packages = new Dictionary<string, ExecutorInfo>();
        static Core()
        {
            string config = ConfigurationManager.AppSettings["packages"];
            if (!String.IsNullOrEmpty(config))
            {
                string[] items = config.Split(';');
                string baseDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Components\\";
                foreach (var name in items)
                {
                    try
                    {
                        Assembly assembly = Assembly.LoadFile(baseDir + name);
                        Type[] types = assembly.GetTypes();
                        foreach (var type in types)
                        {
                            var baseType = type.BaseType;
                            if (baseType == typeof(IAutoProcess))
                            {
                                var instance = Activator.CreateInstance(type);
                                var filed = type.BaseType.GetProperty("Interval");
                                var value = filed.GetValue(instance, null);
                                packages.Add(type.FullName, new ExecutorInfo()
                                {
                                    Type = type,
                                    Name = type.FullName,
                                    Instance = instance,
                                    CurrentTime = Int32.Parse(value.ToString()),
                                    Interval = Int32.Parse(value.ToString())
                                });
                                LogHelper.Write(CommonLogger.Service, LogLevel.Info, "成功加载：" + type.FullName);
                                
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        LogHelper.Write(CommonLogger.Service, LogLevel.Error, e); 
                    }
                }
            }
        }
        public static void OnStart()
        {
            Timer time = new Timer();
            time.Interval = 5 * 60 * 1000;           
            time.Elapsed += (o, e) =>
            {
                foreach (var component in packages)
                {
                    component.Value.CurrentTime -= 1;
                    if (component.Value.CurrentTime == 0)
                    {
                        LogHelper.Write(CommonLogger.Service, LogLevel.Info,
                            String.Format("{0} 于 {1} 被执行", component.Key, DateTime.Now.ToString()));

                        MethodInfo info = component.Value.Type.GetMethod("Execute");
                        info.Invoke(component.Value.Instance, new object[] { DateTime.Now });
                        component.Value.CurrentTime = component.Value.Interval;
                    }
                }
            };
            time.Start();
        }
    }
}
